package com.itheima.app.manager;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service// spring的
public class SettingManager {

    @Reference
    private QuestionService questionService;

    @Reference
    private NotificationService notificationService;

    @Reference
    private BlackListService blackListService;

    public ResponseEntity findSetting() {
        // 1.获取线程内的user对象
        User user = UserHolder.get();

        // 2.查询陌生人问题
        Question question = questionService.findByUserId(user.getId());

        // 3.查询推送通知
        Notification notification = notificationService.findByUserId(user.getId());

        // 4.封装vo对象
        SettingVo vo = new SettingVo();
        // 4-1 设置用户
        vo.setId(user.getId());
        vo.setPhone(user.getPhone());
        // 4-2 设置陌生人问题
        if (question != null) {
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }
        // 4-3 设置推送
        if (notification != null) {
            vo.setGonggaoNotification(notification.getGonggaoNotification());
            vo.setLikeNotification(notification.getLikeNotification());
            vo.setPinglunNotification(notification.getPinglunNotification());
        }

        // 5.返回vo
        return ResponseEntity.ok(vo);
    }

    public ResponseEntity setQuestion(String content) {
        // 1.获取用户id
        Long userId = UserHolder.get().getId();

        // 2.根据用户id查询
        Question question = questionService.findByUserId(userId);

        // 3.判断是否存在
        if (question == null) { // 新增
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            questionService.save(question);
        } else { // 修改
            question.setStrangerQuestion(content);
            questionService.update(question);
        }

        return ResponseEntity.ok(null);
    }

    public ResponseEntity setNotification(Notification notificationParam) {
        // 1.获取userId
        Long userId = UserHolder.get().getId();

        // 2.根据userId查询
        Notification notification = notificationService.findByUserId(userId);

        // 3.判断
        if (notification == null) { // 新增
            notificationParam.setUserId(userId); // 设置用户id
            notificationService.save(notificationParam);
        } else { // 修改
            notificationParam.setId(notification.getId()); // 设置主键
            notificationService.update(notificationParam);

        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        // 1.获取userId
        Long userId = UserHolder.get().getId();

        // 2.分页查询
        PageBeanVo pageBeanVo = blackListService.findByPage(pageNum, pageSize, userId);

        // 3.返回结果
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity deleteBlackList(Long blackId) {
        // 1.获取userId（当前登录人）
        Long userId = UserHolder.get().getId();

        // 2.调用rpc，黑名单移出
        blackListService.deleteByUserIdAndBlackId(userId, blackId);

        return ResponseEntity.ok(null);
    }
}
